package handler

import (
	"2301house/api/request"
	"2301house/api/resp"
	"2301house/consts"
	"2301house/internal/dao"
	"2301house/internal/model"
	"2301house/internal/queue"
	"github.com/gin-gonic/gin"
	"log"
	"path/filepath"
)

func AddHouse(c *gin.Context) {
	var req request.AddHose
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.UType == 3 || user.UType == 2) {
		resp.Error(c, 10000, "权限不足，无法添加房源", nil)
		return
	}
	var house model.House
	dao.GetOneByFields(&model.House{
		Title: req.Title,
	}, &house)
	if house.Id != 0 {
		resp.Error(c, 10000, "当前房源已存在", nil)
		return
	}
	result := dao.Create(&model.House{
		Title:      req.Title,
		Price:      req.Price,
		Area:       req.Area,
		City:       req.City,
		Address:    req.Address,
		Community:  req.Community,
		ImageUrl:   req.ImageUrl,
		VideoUrl:   req.VideoUrl,
		HType:      req.HType,
		FromUserId: c.GetUint("userId"),
	})
	if !result {
		resp.Error(c, 10000, "添加房源失败", nil)
		return
	}
	resp.Success(c, "添加房源成功", nil)

}

func PageHouse(c *gin.Context) {
	var req request.PageHouse
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}
	var house []model.House
	dto := dao.PageDto{
		Page:     req.Page,
		PageSize: req.PageSize,
		OrderBy:  "id desc",
	}

	paginate, err := dao.Paginate(&model.House{
		Title: req.Title,
	}, dto, &house)
	if err != nil {
		resp.Error(c, 10000, "房源列表失败", nil)
		return
	}
	resp.Success(c, "房源列表成功", paginate)

}

func Detail(c *gin.Context) {
	var req request.Detail
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}
	var house model.House
	result := dao.GetOneById(req.Id, &house)
	if !result {
		resp.Error(c, 10000, "房源信息详情失败", nil)
		return
	}
	resp.Success(c, "房源信息详情成功", house)

}

func UpdateHouseType(c *gin.Context) {
	var req request.UpdateHouseType
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	if !(userId == 2 || userId == 3) {
		resp.Error(c, 10000, "没有权限修改", nil)
		return
	}

	var house model.House
	dao.GetOneById(req.Id, &house)
	if house.Id == 0 {
		resp.Error(c, 10000, "当前房源不存在", nil)
		return
	}

	result := dao.Update(&model.House{
		Id:    house.Id,
		HType: 4, //当房屋状态为4时，房源被锁定
	})
	if !result {
		resp.Error(c, 10000, "修改房源状态失败", nil)
		return
	}
	resp.Success(c, "修改房源状态成功", nil)

}

func UploadHouseUrl(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}
	log.Println(file.Filename)
	dst := "D:\\gowork\\src2\\2301house\\static\\upoload\\" + file.Filename
	err = c.SaveUploadedFile(file, dst)
	if err != nil {
		resp.Error(c, 10000, "上传失败", nil)
		return
	}
	resp.Success(c, "上传成功", "http://127.0.0.1:8080/upload/"+file.Filename)
}

func UploadHouseVideo(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}
	dst := "D:\\gowork\\src2\\2301house\\static\\upoload\\" + file.Filename

	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" {
		resp.Error(c, 10000, "限制格式为 mp4", nil)
		return
	}
	if file.Size > 200*1024*1024 {
		resp.Error(c, 10000, "最大 200MB", nil)
		return
	}
	err = c.SaveUploadedFile(file, dst)
	if err != nil {
		resp.Error(c, 10000, "上传失败", nil)
		return
	}
	resp.Success(c, "上传成功", "http://127.0.0.1:8080/upload/"+file.Filename)

}

func UpdateHousePrice(c *gin.Context) {
	var req request.UpdateHousePrice
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}
	var user model.User
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if !(user.UType == 2 || user.UType == 3) {
		resp.Error(c, 10000, "没有权限修改", nil)
		return
	}

	var house model.House
	dao.GetOneById(req.Id, &house)
	if house.Id == 0 {
		resp.Error(c, 10000, "当前房源不存在", nil)
		return
	}
	if req.Price > 0 && (req.Price != house.Price) {
		go queue.PublishPub(consts.LILUO, model.House{
			Id:    house.Id,
			Price: req.Price,
		})
	}
	dao.Update(&model.House{
		Id:    req.Id,
		Price: req.Price,
	})
	resp.Success(c, "修改房源价格成功", nil)

}
